using System;
using Engine;
using Engine.Graphics;
using Game;

namespace RuthlessConquest {
    public class BusyBarWidget : Widget {
        public const int m_barsCount = 5;

        public const float m_barSize = 10f;

        public const float m_barsSpacing = 24f;

        public int m_boxIndex;

        public double m_lastBoxesStepTime;

        public FlatBatch2D m_batch;

        public Color LitBarColor { get; set; }

        public Color UnlitBarColor { get; set; }

        public BusyBarWidget() {
            IsHitTestVisible = false;
            LitBarColor = Colors.Fore;
            UnlitBarColor = Colors.ForeDark;
        }

        public override void MeasureOverride(Vector2 parentAvailableSize) {
            IsDrawRequired = true;
            DesiredSize = new Vector2(120f, 14f);
        }

        public override void Draw(DrawContext dc) {
            if (Time.FrameStartTime - m_lastBoxesStepTime > 0.25) {
                m_boxIndex++;
                m_lastBoxesStepTime = Time.FrameStartTime;
            }
            m_batch ??= dc.PrimitivesRenderer2D.FlatBatch(0, DepthStencilState.None);
            float num = 2f * (float)MathUtils.Remainder(Time.FrameStartTime, 10000.0);
            int count = m_batch.TriangleVertices.Count;
            for (int i = 0; i < 5; i++) {
                Vector2 center = new((i + 0.5f) * 24f, 7f);
                Color color = i == m_boxIndex % 5 ? LitBarColor : UnlitBarColor;
                float num2 = i == m_boxIndex % 5 ? 14f : 10f;
                m_batch.QueueDisc(
                    center,
                    new Vector2(num2 / 2f),
                    0f,
                    color * GlobalColorTransform,
                    5,
                    num,
                    num + (float)Math.PI * 2f
                );
                m_batch.QueueDisc(
                    center,
                    new Vector2(num2 / 2f + 1f),
                    new Vector2(num2 / 2f),
                    0f,
                    Color.Transparent,
                    color * GlobalColorTransform,
                    5,
                    num,
                    num + (float)Math.PI * 2f
                );
            }
            m_batch.TransformTriangles(GlobalTransform, count);
        }
    }
}